<?php
namespace app\shop\model;

use cubox\Model;
use cubox\model\Tree;
class SpecItem extends Model{
    use Tree;

    protected $spec = 0;
    
    public function setSpec($spec_id){
        $this->spec = $spec_id;
        $this->treeCacheName = $this->table.'_'.$spec_id.'_tree';
    }

    protected function initTreeData(){
        if ($this->treeSearchType == 'ALL' && empty(self::$treeAllData)){ 
            self::$treeAllData = $this->where('spec_id',$this->spec)->order($this->treeOrderKey,$this->treeOrderDir)->getData();   
        }
    }

    protected function filterTreeData($data){
        
        if ($data['type']=='select'){
            try{
                $data['options'] = json_decode($data['options'],true);
            }catch(\Exception $e){
                $data['options'] = array();
            }
        }
        
        return $data;
    }
}